<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"%>
<%@ page import="org.apache.shiro.authc.ExcessiveAttemptsException"%>
<%@ page import="org.apache.shiro.authc.IncorrectCredentialsException"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta charset="utf-8" />
		<title>登录</title>

		<meta name="description" content="User login page" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

		<!-- bootstrap & fontawesome -->
		<link rel="stylesheet" href="${ctx}/static/css/bootstrap.min.css" />
		<link rel="stylesheet" href="${ctx}/static/css/font-awesome.min.css" />
		
		<link rel="stylesheet" href="${ctx}/static/css/validate.css" />

		<!-- text fonts -->
		<link rel="stylesheet" href="${ctx}/static/css/ace-fonts.min.css" />

		<!-- ace styles -->
		<link rel="stylesheet" href="${ctx}/static/css/ace.min.css" />

		<!--[if lte IE 9]>
			<link rel="stylesheet" href="${ctx}/static/css/ace-part2.css" />
		<![endif]-->
		<link rel="stylesheet" href="${ctx}/static/css/ace-rtl.min.css" />

		<!--[if lte IE 9]>
		  <link rel="stylesheet" href="${ctx}/static/css/ace-ie.min.css" />
		<![endif]-->

		<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->

		<!--[if lt IE 9]>
		<script src="${ctx}/static/js/html5shiv.min.js"></script>
		<script src="${ctx}/static/js/respond.min.js"></script>
		<![endif]-->
	</head>

	<body class="login-layout light-login">
		<div class="main-container">
			<div class="main-content">
				<div class="row">
					<div class="col-sm-10 col-sm-offset-1">
						<div class="login-container">
							<div class="center">
								<h1>
									<i class="ace-icon fa fa-leaf green"></i>
									<span class="red"></span>
									<span class="grey" id="id-text2">后台管理系统</span>
								</h1>
								<h4 class="blue" id="id-company-text">&copy; 科技</h4>
							</div>

							<div class="space-6"></div>

							<div class="position-relative">
								<div id="login-box" class="login-box <c:if test="${tag != '1'}"> visible </c:if> widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header blue lighter bigger">
												<i class="ace-icon fa fa-coffee green"></i>
												请输入登录信息
											</h4>

											<div class="space-6"></div>

											<form id="loginForm" method="post">
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="text" autocomplete="off" id="username" name="username"  value="${username}" class="form-control required" placeholder="登录名" />
															<i class="ace-icon fa fa-user"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="password" id="password" name="password" class="form-control required" placeholder="密码" />
															<i class="ace-icon fa fa-lock"></i>
														</span>
													</label>

													<div class="space"></div>

													<div class="clearfix">
<!-- 														<label class="inline"> -->
<!-- 															<input type="checkbox" class="ace" id="rememberMe" name="rememberMe"/> -->
<!-- 															<span class="lbl"> 记住我</span> -->
<!-- 														</label> -->

														<button id="login" type="button" class="width-35 pull-right btn btn-sm btn-primary">
															<i class="ace-icon fa fa-key"></i>
															<span class="bigger-110">登录</span>
														</button>
													</div>

													<div class="space-4"></div>
												</fieldset>
											</form>

										
										</div><!-- /.widget-main -->

										<div class="toolbar clearfix">
											<div>
												<a href="#" data-target="#forgot-box" class="forgot-password-link">
													<i class="ace-icon fa fa-arrow-left"></i>
													忘记密码
												</a>
											</div>
										
										</div>
									</div><!-- /.widget-body -->
								</div><!-- /.login-box -->

								<div id="forgot-box" class="forgot-box widget-box no-border <c:if test="${tag == '1'}"> visible </c:if> ">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header red lighter bigger">
												<i class="ace-icon fa fa-key"></i>
												找回密码
											</h4>

											<div class="space-6"></div>
											<p>
												输入您的手机号并发送
											</p>

											<form id="passwordForm" action="${ctx}/retrieve" method="post">
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input id="mobile" autocomplete="off" name="mobile" type="text" class="form-control required" rangelength:[11,11] placeholder="手机号" />
															<i class="ace-icon glyphicon glyphicon-phone"></i>
														</span>
													</label>
													
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input id="code" autocomplete="off" name="code" type="text" class="form-control required" placeholder="验证码" />
															<i class="ace-icon glyphicon glyphicon-flash"></i>
														</span>
													</label>
													
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input id="plainPassword" autocomplete="off" name="plainPassword" type="password" class="form-control required" placeholder="密码" />
															<i class="ace-icon fa fa-lock"></i>
														</span>
													</label>
													
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input id="confirmPassword" autocomplete="off" name="confirmPassword" type="password" class="form-control required" placeholder="确认密码" equalTo="#plainPassword" />
															<i class="ace-icon fa fa-retweet"></i>
														</span>
													</label>
													
													<div class="space-24"></div>
													
													<div class="clearfix">
														<button type="button" class="width-30 pull-left btn btn-sm btn-danger" onclick="sendCode()">
															<i class="ace-icon fa fa-lightbulb-o"></i>
															<span class="bigger-110">发送！</span>
														</button>

														<button type="submit" class="width-65 pull-right btn btn-sm btn-success">
															<span class="bigger-110">提交</span>

															<i class="ace-icon fa fa-arrow-right icon-on-right"></i>
														</button>
													</div>

												</fieldset>
											</form>
										</div><!-- /.widget-main -->

										<div class="toolbar center">
											<a href="#" data-target="#login-box" class="back-to-login-link">
												返回登录
												<i class="ace-icon fa fa-arrow-right"></i>
											</a>
										</div>
									</div><!-- /.widget-body -->
								</div><!-- /.forgot-box -->
<%-- 								<c:if test="${not empty message}"> --%>
<%-- 									<div id="message" class="alert alert-success">${message}</div> --%>
<%-- 								</c:if>	 --%>
									<div id="message" class="alert alert-success"></div>

								
							</div><!-- /.position-relative -->
							
						</div>
					</div><!-- /.col -->
				</div><!-- /.row -->
			</div><!-- /.main-content -->
		</div><!-- /.main-container -->

		<!-- basic scripts -->

		<!--[if !IE]> -->
		<script type="text/javascript">
			window.jQuery || document.write("<script src='${ctx}/static/js/jquery.min.js'>"+"<"+"/script>");
		</script>

		<!-- <![endif]-->

		<!--[if IE]>
<script type="text/javascript">
 window.jQuery || document.write("<script src='${ctx}/static/js/jquery1x.min.js'>"+"<"+"/script>");
</script>
<![endif]-->
		<script type="text/javascript">
			if('ontouchstart' in document.documentElement) document.write("<script src='${ctx}/static/js/jquery.mobile.custom.min.js'>"+"<"+"/script>");
		</script>

		<!-- inline scripts related to this page -->
		
		<script src="${ctx}/static/js/jquery.validate.min.js"></script>
		<script src="${ctx}/static/js/messages_bs_zh.js"></script>
		
		<script type="text/javascript">
			jQuery(function($) {
			 $(document).on('click', '.toolbar a[data-target]', function(e) {
				e.preventDefault();
				var target = $(this).data('target');
				$('.widget-box.visible').removeClass('visible');//hide others
				$(target).addClass('visible');//show target
			 });
			 
			 $(function(){				 
				//回车事件绑定
				document.onkeydown=function(event){
					var e = event || window.event || arguments.callee.caller.arguments[0];
					if(e && e.keyCode==13){ 
						$('#login').click();
					}
				}; 
			 })
			 
				//登录操作
			    $('#login').click(function(){	    	
			        var username = $('#username').val();
			        var password = $('#password').val();
			        if(username == '') {
			            $('#message').html("用户名不能为空")
			            return false;
			        }
			        if(password == '') {
			        	 $('#message').html("密码不能为空");
			            return false;
			        }
			        data = {password:password,loginName:username,rememberMe:$("#rememberMe").is(':checked')};
			    	$.ajax({
						type:'post',
						url:'${ctx}/retrieve/submitLogin',
						async:false,
						data:data,
						success:function(result) {
				    		if(result && result.status != 200){
				    			$('#password').val('');
				    			$('#message').html(result.message);
				    			return;
				    		}else{
				    			setTimeout(function(){
				    				//登录返回
					    			window.location.href= '${ctx}'+result.back_url || "/";
				    			},1000)
				    		}
						}
					});			        
			    });
			});
			
			$(document).ready(function() {
				//聚焦第一个输入框
				$("#username").focus();
				
				$("#loginForm").validate({
					rules:{
						username:{
							required:true
						},
						password:{
							required:true
						}
					}
				});
				$("#passwordForm").validate({
					rules: {
						mobile: {
							rangelength:[11,11],
							digits:true
						}
					},
					messages: {
						mobile: {
							rangelength: "手机格式不正确"
						}
					}
				});
			});
			
			function sendCode() {
				var mobile_format = /^[1][0-9]{10}$/;
			    if (mobile_format.test($('#mobile').val())) {
			    	$.ajax({
						type:'get',
						url:'${ctx}/retrieve/sendCode',
						data:'mobile=' + $('#mobile').val(),
						success:function(data) {
							if(data == 0) {
								alert('验证码已发送！');
							}else {
								if(data == -1){
									alert("发送失败，请联系系统维护人员");
								}else{									
									alert('手机号不存在！');
								}
							}
							
						}
					});
			    }else {
			    	alert("手机格式输入错误！");
			    }
				
			};
			
		</script>
	</body>
</html>
